package com.baomidou.springboot.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.springboot.entity.User;
import com.baomidou.springboot.hystrix.GetShopInfoFromReidsCacheCommand;
import com.baomidou.springboot.hystrix.SaveShopInfo2ReidsCacheCommand;
import com.baomidou.springboot.util.RedisUtil;
import com.baomidou.springboot.util.SpringUtils;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 资源控制器
 * 用来演示不同角色访问该控制器的接口返回的信息
 */
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {
    /**
     * http://localhost:8080/test/hello
     * 只有 ROLE_USER 角色的用户才能访问
     * @return 问候信息
     */
    @GetMapping("/hello")
    @HystrixCommand(groupKey = "helloGroup", commandKey = "hello" ,fallbackMethod = "hellFallback")
    public String helloUser(Integer val) throws Exception {
        if(val==1) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }
            throw new Exception();
        }
        if(val==2) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
        }
        if(val==3) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }
        }
        return "hello User";
    }
    public String hellFallback(Integer val){
        return "hellFallback";
    }
    /**
     * http://localhost:8080/test/adduser?id=
     */
    @GetMapping("/adduser")
    public String adduser(Long id) throws Exception {
        User user = new User();
        user.setId(id);
        user.setName("测试");
        user.setAge(11);
        SaveShopInfo2ReidsCacheCommand saveShopInfo2ReidsCacheCommand = new SaveShopInfo2ReidsCacheCommand(user);
        saveShopInfo2ReidsCacheCommand.execute();
        return "adduser success";
    }

    /**
     * http://localhost:8080/test/getuser
     * 100 200并发正常执行 平均速度10ms 其实就是1线程一秒能执行100并发.但是随着并发加大 执行速度会减慢
     */
    @GetMapping("/getuser")
    public String getuser(Long id) throws Exception {
        GetShopInfoFromReidsCacheCommand getShopInfoFromReidsCacheCommand = new GetShopInfoFromReidsCacheCommand(id);
        User user = getShopInfoFromReidsCacheCommand.execute();
        return user.getName();
    }
    /**
     * http://localhost:8080/test/getredisuser
     * 100 200并发正常执行 平均速度10ms 其实就是1线程一秒能执行100并发.但是随着并发加大 执行速度会减慢
     */
    @GetMapping("/getredisuser")
    public String getredisuser(Long id) throws Exception {
        RedisUtil redisUtil = (RedisUtil) SpringUtils
                .getBean("redisUtil");
        String key = "shop_info_" + id;
        String user =  redisUtil.get(key,0);
        if(user != null) {
            return user;
        }
        return null;
    }
}